package ATM;

import java.util.Scanner;

public class ATM03 {
    /**
     * 方法版本的ATM
     */
        public static Scanner sc = new Scanner(System.in);
        public static String [][] users = {
                {"aaa","111","1000"},
                {"bbb","222","2000"},
                {"ccc","333","3000"},
                {"ddd","444","4000"},
                {"eee","555","5000"}
        };
        public static int index;

        /**
         * 主流程方法
         */
        public static void process(){

            System.out.println("欢迎使用XXX银行");
            login();
            menu();
        }
        /**
         * 登录
         */
        public static void login(){
        for(int i = 0;i < 3;i++){
            //登录判断
            System.out.println("请输入账号");
            String account = sc.next();
            System.out.println("请输入密码");
            String pwd = sc.next();
            boolean isLogin = false;
            for (int j = 0;j < users.length;j++){
                if (users[j][0].equals(account) && users[j][1].equals(pwd)){
                    System.out.println("登录成功");
                    isLogin = true;
                    index = j;
                    break;
                }
            }
            if(isLogin){
                break;
            }else {
                System.out.println("登录失败，还剩" + (2 - i) +"次机会");
                if(i == 2){
                    System.out.println("账户冻结");
                    System.exit(0);
                }
            }
        }
                menu();
        }


        /**
         * 菜单
         */
        public static void menu(){
            System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-切换账号 6-退出 7-修改密码");
            int input = sc.nextInt();
            if(input == 1){
                save();
            }else if(input == 2){
                get();
            }else if (input == 3){
                search();
            }else if (input == 4){
                transfer();
            }else if (input == 5){
                changeAcc();
            }else if (input == 6){
                System.out.println("再见");
                System.exit(0);
            }else if (input == 7){
                changePwd();
            }else {
                System.out.println("输入有误");
            }
        }

        /**
         * 存款
         */
        public static void save(){
            System.out.println("请输入存款金额：");
            double blance = sc.nextDouble();
            users[index][2] = Double.parseDouble(users[index][2]) + blance  + "";
            System.out.println("存款成功");
            menu();
        }

        /**
         * 取款
         */
        public static void get(){
            System.out.println("请输入取款金额");
            double blance = sc.nextDouble();
            if(blance > Double.parseDouble(users[index][2])){
                System.out.println("余额不足");
            }else{
                users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
                System.out.println("取款成功");
            }
            menu();
        }

        /**
         * 查询余额
         */
        public static void search(){
            System.out.println("您的余额为："+users[index][2]);
            menu();
        }

        /**
         * 转账
         */
        public static void transfer(){
            System.out.println("请输入对方账号：");
            String otherAccount = sc.next();
            // 不能转账给自己
            if(otherAccount.equals(users[index][0])){
                System.out.println("不能转给自己");
                menu();
            }
            // 判断对方账号是否存在
            boolean isExist = false;
            int otherIndex = 0;
            for(int i = 0;i < users.length;i++){
                if(users[i][0].equals(otherAccount)){
                    isExist = true;
                    otherIndex = i;
                    break;
                }
            }
            if(!isExist){
                System.out.println("输入的对方账号错误");
                menu();
            }
            System.out.println("请输入转账金额：");
            double blance = sc.nextDouble();
            // 判断余额是否足够
            if(blance > Double.parseDouble(users[index][2])){
                System.out.println("余额不足");
                menu();
            }
            // 从当前账号上扣去转账的金额
            users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
            // 将转账的金额加在对方账号上
            users[otherIndex][2] = Double.parseDouble(users[otherIndex][2]) + blance + "";

            System.out.println("转账成功");
        }

        /**
         * 修改密码
         */
        public static void changeAcc(){
            login();
        }


        public static void changePwd(){
            System.out.println("请输入原密码");
            String Pwd1 = sc.next();
            boolean chn = false;
            if(Pwd1.equals(users[index][1])) {
                chn = true;
            }else {
                System.out.println("原密码不正确");
                return;
            }
            if(chn){
                System.out.println("请输入新密码");
                String newPwd = sc.next();
                System.out.println("请再次确定您的新密码");
                String newPwd1 =sc.next();
                if(newPwd1.equals(newPwd)){
                    System.out.println("密码修改成功");
                    users[index][1] = newPwd1;
                }else {
                    System.out.println("确认密码输入错误，无法修改");
                    return;
                }
                login();
            }
        }

        public static void main(String[] args) {
            process();
        }
    }


